package com.woniuxy.controller;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.DoctorDao;
import com.woniuxy.entity.Doctor;
import com.woniuxy.entity.Equipment;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DoctorController {

    @Autowired
    DoctorService doctorService;

    //路径变量参数
    //doctor/{pageIndex}/{pageSize}
    @PostMapping("/doctor/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Doctor>> getAll(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize,
                                                   @RequestBody Doctor doctor) {
        ResponseResult<PageBean<Doctor>> responseResult;
        try {
            PageBean<Doctor> pageBean = doctorService.getAllDoctors(doctor, pageIndex, pageSize);
            responseResult = new ResponseResult<>(200, "ok", pageBean);

            return responseResult;
        } catch (serviceException e) {
            responseResult = new ResponseResult<>(500, e.getMessage(), null);
            return responseResult;
        }
    }

    @DeleteMapping("/doctor/{doctorId}")

    public void deleteDoctor(@PathVariable("doctorId") int doctorId) {

        doctorService.removeDoctor(doctorId);
    }


    @PostMapping("/doctor")
    public void insertDoctor(@RequestBody Doctor doctor) {

        doctorService.addDoctor(doctor);

    }


    @PutMapping("/doctor")
    public void updateDoctor(@RequestBody Doctor doctor) {
        doctorService.updateDoctor(doctor);
    }

    @GetMapping("/doctorName/{departmentsName}")
    public List<Doctor> getDoctorName(@PathVariable String departmentsName) {
        System.out.println(departmentsName + "******" + (departmentsName == null));
        return doctorService.selectByDepartments(departmentsName);
    }


}
